﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace Googlempas_Form
{
    class Database_CN
    {
        private static string StrKetnoi = @"Data Source=JOSEPH-PC\SQLEXPRESS;Initial Catalog=DB_from_phone; Integrated Security=True";
        public static SqlConnection CN;
        public static string ChuoiKetnoi()
        {
            return StrKetnoi;
        }
        public static void ThietlapKetnoi()
        {
            try
            {
                CN = new SqlConnection(StrKetnoi);
                CN.Open();

            }
            catch
            {

            }
        }
        public static void HuyKetnoi()
        {
            CN.Close();
        }
        //Lấy dữ liệu từ bảng
        public DataTable TaoBang(string sql)
        {

            ThietlapKetnoi();
            SqlDataAdapter Adapter = new SqlDataAdapter(sql, CN);
            DataTable DTable = new DataTable();
            Adapter.Fill(DTable);
            Adapter.Update(DTable);
            return DTable;

        }
        //Ham thuc hien lenh executenonquery
        public void ExcuteNonQuery(string sql)
        {
            //Ket noi Database
            ThietlapKetnoi();
            //Thực hiện insert
            SqlCommand cmd = new SqlCommand(sql, CN);
            cmd.ExecuteNonQuery();
            //Hủy kết nối Database
            HuyKetnoi();
            cmd.Dispose();
        }
    }
}
